home *** CD-ROM | disk | FTP | other *** search
- Path: news.eclipse.net!usenet
- From: steve@eclipse.net (Steve Teale)
- Newsgroups: comp.lang.c++
- Subject: Re: C++ help--probably a simple answer (short listing attached)
- Date: Mon, 05 Feb 1996 16:56:55 GMT
- Message-ID: <4f5cup$26u@lunar.eclipse.net>
- References: <4ehdkl$mj0@ixnews7.ix.netcom.com>
- NNTP-Posting-Host: ne_36.eclipse.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- mearrin@ix.netcom.com(Michael Arrington ) wrote:
-
- >Hi,
-
- >I have a question about C++. I'm just learning C++ but I've been
- >writing programs in other languages for a number of years. In this
- >problem, I'm creating a class to represent a 2D cellular automaton
- >(like Conway's Life). Here's the (partial) declaration and
- >implementation of the class:
-
- >*******************
- >****FROM CELL.H****
- >*******************
-
- >class CCellAuto
- >{
- >public:
- > // Public constructors & destructors
- > CCellAuto(); // Default constructor
- > CCellAuto(int xSize, int ySize); // Overloaded
- >Constructor
- > ~CCellAuto(); // Default Destructor
- > // Public accessor methods
- > void BumpGen();
- > void BumpPop();
- > int GetGen() const;
- > int GetPop() const;
- > void RandomizeCells();
- > void SetRules(int deathLow, int deathHigh, int birthLow, int
- >birthHigh);
- > void UpdateCells();
- > void ResizeArrays(int xSize, int ySize);
-
- >private:
- > // Private data members
- > int xSize; // X dimension of arrays
- > int ySize; // Y dimension of arrays
- > int cell[1][1]; // Array holding live cells
- > int old[1][1]; // Temp array
- > int deathLow; // Birth/Death rules
- > int deathHigh;
- > int birthLow;
- > int birthHigh;
- > int currGen; // # of current generation
- > int currPop; // # of cells alive
- > int wrap; // Wrap to other side of array?
- >};
-
- >*********************
- >****FROM CELL.CPP****
- >*********************
-
- >// Default constructor
- >CCellAuto::CCellAuto()
- >{
- > wrap = 1; // Set wrap to on
- > currPop = 0; // Initialize currPopulation
- > currGen = 1; // Initialize currGeneration
- > ResizeArrays(10, 10); // Set default array size
- > SetRules(2, 4, 2, 4); // Set default rules
- > RandomizeCells(); // Randomize cells
- >}
-
- >// Overloaded Constructor
- >CCellAuto::CCellAuto(int x, int y)
- >{
- > wrap = 1; // Set wrap to on
- > currPop = 0; // Initialize currPopulation
- > currGen = 1; // Initialize currGeneration
- > ResizeArrays(x, y); // Size arrays to X by Y
- > SetRules(2, 4, 2, 4); // Set default rules
- > RandomizeCells(); // Randomize cells
- >}
-
- >// Default Destructor
- >CCellAuto::~CCellAuto()
- >{
- >}
-
- >// BumpGen()
- >void CCellAuto::BumpGen()
- >{
- > currGen++;
- >}
-
- >// BumpPop()
- >void CCellAuto::BumpPop()
- >{
- > currPop++;
- >}
-
- >// GetGen()
- >int CCellAuto::GetGen() const
- >{
- > return currGen;
- >}
-
- >// GetPop()
- >int CCellAuto::GetPop() const
- >{
- > return currPop;
- >}
-
- >// RandomizeCells()
- >void CCellAuto::RandomizeCells()
- >{
- > // Omitted...Randomly sets cell[][] elements to either 1 or 0
- >}
-
- >// ResizeArrays()
- >void CCellAuto::ResizeArrays(int x, int y)
- >{
- > // Omitted...Sets cell[][] and old[][] to dimensions x and y
- >}
-
- >// SetRules()
- >void CCellAuto::SetRules(int a, int b, int c, int d)
- >{
- > deathLow = a;
- > deathHigh = b;
- > birthLow = c;
- > birthHigh = d;
- >}
-
- >// UpdateCells()
- >void CCellAuto::UpdateCells()
- >{
- > // Omitted...copies cell[][] to old[][] and creates next iteration
- > // in new[][]. Calls BumpGen() when done.
- >}
-
- >Whew! I omitted a lot of that...but I wanted to give enough to let you
- >see any problems I might have created in it. Now, when I declare a
- >CCellAuto object [[like this -- CCellAuto *Life1 = new CCellAuto(20,
- >20); ]]and try to access a member function [[like this -- cout <<
- >Life1->GetPop() << endl; ]] I get uninitialized variables. That's
- >weird because I initialize them in the constructor. Now, if I were to
- >call Life1->BumpPop() first...the GetPop() function returns the correct
- >number. Why? Here's my main() listing:
-
- >*********************
- >****FROM LIFE.CPP****
- >*********************
-
- >01 void main()
- >02 {
- >03 CCellAuto *Life1 = new CCellAuto(20,20);
- >04 if (Life1 == 0)
- >05 cout << "cannot allocate memory\n";
- >06 //Life1->BumpPop();
- >07 cout << "Life1 Pop: " << Life1->GetPop() << endl;
- >08 cout << "Life1 Gen: " << Life1->GetGen() << endl;
- >09 delete Life1;
- >10 }
-
-
- >As listed, lines 07 and 08 return 0 values for currPop and currGen. If
- >you remove the comment marks from line 06, line 07 will return the
- >actual (albeit incremented) value of 1. I have no idea what is going on
- >here. I don't think it's my logic--I've written this same program
- >before in BASIC and some other languages. Any suggestions as to what's
- >going on? Please help!
-
- >If you have any answers, please e-mail to MEArrin@ix.netcom.com unless
- >you feel it would be beneficial to the forum. Thanks in advance for
- >your help.
-
- >Mike
-
- >BTW: I'm using MSC++ v7.0 if that's a help.
-
- Your problem is probably in ResizeArrays, which you omitted. You
- can't do it using the class definition you propose.
-
-